<template>
  <div :style="{ ...coreStyle.marginSame(8) }">
    <a-row :gutter="8">
      <a-col :span="12">
        <a-card>
          <a-form ref="dictFormQueryRef" :model="dictFormQueryModel" labelAlign="left" layout="vertical" :style="{ ...coreStyle.marginBottom(10) }">
            <a-row :gutter="16" v-show="!dictFormQueryVisible">
              <a-col :span="8">
                <a-form-item :style="{ ...coreStyle.marginBottom(-8) }">
                  <a-input-search v-model:value="dictFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="dictFormQuerySubmitting" @search="dictFormQuerySearch" />
                </a-form-item>
              </a-col>
              <a-col :span="16">
                <a-button type="link" :style="{ ...coreStyle.floatRight }" @click="dictFormQueryUpDown">
                  展开
                  <CoreIcon icon="akar-icons:chevron-down" :size="14" />
                </a-button>
              </a-col>
            </a-row>
            <a-row :gutter="16" v-show="dictFormQueryVisible">
              <a-col :span="8">
                <a-form-item>
                  <a-input-search v-model:value="dictFormQueryModel.keySearchVo" placeholder="关键字搜索" :disabled="dictFormQuerySubmitting" @search="dictFormQuerySearch" />
                </a-form-item>
              </a-col>
              <a-col :span="16">
              </a-col>
              <a-col :span="8">
                <a-form-item label="数据字典标识">
                  <a-input v-model:value="dictFormQueryModel.dictKeyAndLike" placeholder="输入数据字典标识" />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="数据字典名称">
                  <a-input v-model:value="dictFormQueryModel.dictNameAndLike" placeholder="输入数据字典名称" />
                </a-form-item>
              </a-col>
              <a-col :span="24">
                <a-space :style="{ ...coreStyle.floatRight }">
                  <a-button @click="dictFormQueryReset">
                    <CoreIcon icon="ant-design:clear-outlined" :size="14" />
                    重置
                  </a-button>
                  <a-button type="primary" ghost :loading="dictFormQuerySubmitting" @click="dictFormQuerySearch">
                    <CoreIcon icon="ant-design:search-outlined" :size="14" />
                    查询
                  </a-button>
                  <a-button type="link" @click="dictFormQueryUpDown">
                    收起
                    <CoreIcon icon="akar-icons:chevron-up" :size="14" />
                  </a-button>
                </a-space>
              </a-col>
            </a-row>
          </a-form>
          <a-space>
            <a-button type="primary" ghost @click="dictAdd">
              <CoreIcon icon="carbon:add-alt" :size="14" />
              新增
            </a-button>
            <a-button type="dashed" @click="dictUpdate" v-if="dictListSelectLength === 1">
              <CoreIcon icon="ri:brush-line" :size="14" />
              修改
            </a-button>
            <a-popconfirm title="确认删除？" placement="bottom" @confirm="dictDelete" v-if="dictListSelectLength > 0">
              <a-button type="dashed" danger>
                <CoreIcon icon="jam:delete" :size="14" />
                删除
              </a-button>
            </a-popconfirm>
            <a-button @click="dictDetailInfo" v-if="dictListSelectLength === 1">
              <CoreIcon icon="heroicons-outline:light-bulb" :size="14" />
              详情
            </a-button>
          </a-space>
          <a-space :style="{ ...coreStyle.floatRight }">
            <a-button type="link" shape="circle" @click="dictListGetDataSet">
              <CoreIcon icon="ci:refresh" :size="22" />
            </a-button>
          </a-space>
          <a-table
            rowKey="id"
            :columns="dictListColumns"
            :data-source="dictListDataSet"
            :loading="dictListLoading"
            :pagination="false"
            bordered
            :customRow="dictListCustomRow"
            :row-selection="{ fixed: true, selectedRowKeys: dictListSelectedRowKeys, onChange: dictListRowOnChange }"
            :scroll="{ x: '100%', y: 550 }"
            :style="{ ...coreStyle.marginTop(10) }"
          >
            <template #multipleChoiceData="{ text }">
              <span>
                <a-tag color="red" :style="{ 'line-height': '19px' }" v-if="'y' === text">{{ coreToolStore.getDictDisplayName('dictMultipleChoice', text) }}</a-tag>
                <a-tag color="green" :style="{ 'line-height': '19px' }" v-if="'n' === text">{{ coreToolStore.getDictDisplayName('dictMultipleChoice', text) }}</a-tag>
              </span>
            </template>
            <template #valueTypeData="{ text }">
              <span>
                <a-tag color="blue" :style="{ 'line-height': '19px' }" v-if="'list' === text">{{ coreToolStore.getDictDisplayName('dictValueType', text) }}</a-tag>
                <a-tag color="purple" :style="{ 'line-height': '19px' }" v-if="'tree' === text">{{ coreToolStore.getDictDisplayName('dictValueType', text) }}</a-tag>
              </span>
            </template>
          </a-table>
          <a-pagination
            v-model:current="dictListDataPage"
            v-model:pageSize="dictListDataRows"
            :total="dictListDataCount"
            show-size-changer
            show-quick-jumper
            :page-size-options="[ ...coreStyle.antdPaginationPageSizeOptions ]"
            :show-total="(total, range) => coreStyle.antdPaginationShowTotal(total, range)"
            :style="{ ...coreStyle.marginTop(10), ...coreStyle.textAlignCenter }"
            @change="dictListPaginationChange"
            @showSizeChange="dictListPaginationShowSizeChange"
            v-if="dictListPaginationVisible"
          >
          </a-pagination>
          <p :style="{ ...coreStyle.marginTop(14), ...coreStyle.marginBottom(6), ...coreStyle.textAlignCenter }" v-if="!dictListPaginationVisible">
            {{ coreStyle.antdPaginationShowTotal(dictListDataCount) }}
          </p>
        </a-card>
      </a-col>
      <a-col :span="12">
        <a-card>
          <a-space :style="{ ...coreStyle.marginTop(42) }">
            <a-button type="primary" ghost :disabled="!dictListSelectCurrentRowData.id" @click="dictValueAdd">
              <CoreIcon icon="carbon:add-alt" :size="14" />
              新增
            </a-button>
            <a-button type="dashed" @click="dictValueUpdate" v-if="dictValueListSelectLength === 1">
              <CoreIcon icon="ri:brush-line" :size="14" />
              修改
            </a-button>
            <a-popconfirm title="确认删除？" placement="bottom" @confirm="dictValueDelete" v-if="dictValueListSelectLength > 0">
              <a-button type="dashed" danger>
                <CoreIcon icon="jam:delete" :size="14" />
                删除
              </a-button>
            </a-popconfirm>
            <a-button @click="dictValueDetailInfo" v-if="dictValueListSelectLength === 1">
              <CoreIcon icon="heroicons-outline:light-bulb" :size="14" />
              详情
            </a-button>
          </a-space>
          <a-space :style="{ ...coreStyle.floatRight, ...coreStyle.marginTop(42) }">
            <a-button type="link" shape="circle" @click="dictValueFormQuerySearch">
              <CoreIcon icon="ci:refresh" :size="22" />
            </a-button>
          </a-space>
          <a-table
            rowKey="id"
            :columns="dictValueListColumns"
            :data-source="dictValueListDataSet"
            :loading="dictValueListLoading"
            :pagination="false"
            bordered
            :customRow="dictValueListCustomRow"
            :row-selection="{ fixed: true, selectedRowKeys: dictValueListSelectedRowKeys, onChange: dictValueListRowOnChange }"
            :scroll="{ x: '100%', y: 550 }"
            :style="{ ...coreStyle.marginTop(10) }"
          >
          </a-table>
          <a-pagination
            v-model:current="dictValueListDataPage"
            v-model:pageSize="dictValueListDataRows"
            :total="dictValueListDataCount"
            show-size-changer
            show-quick-jumper
            :page-size-options="[ ...coreStyle.antdPaginationPageSizeOptions ]"
            :show-total="(total, range) => coreStyle.antdPaginationShowTotal(total, range)"
            :style="{ ...coreStyle.marginTop(10), ...coreStyle.textAlignCenter }"
            @change="dictValueListPaginationChange"
            @showSizeChange="dictValueListPaginationShowSizeChange"
            v-if="dictValueListPaginationVisible"
          >
          </a-pagination>
          <p :style="{ ...coreStyle.marginTop(14), ...coreStyle.marginBottom(6), ...coreStyle.textAlignCenter }" v-if="!dictValueListPaginationVisible">
            {{ coreStyle.antdPaginationShowTotal(dictValueListDataCount) }}
          </p>
        </a-card>
      </a-col>
    </a-row>
    <a-drawer
      v-model:visible="dictEditDrawerVisible"
      title="编辑数据字典信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="dictEditDrawerOnClose"
    >
      <a-form ref="dictFormRef" :model="dictFormModel" :rules="dictFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="数据字典名称" name="dictName">
              <a-input v-model:value="dictFormModel.dictName" placeholder="输入数据字典名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="数据字典标识 (不能重复)" name="dictKey">
              <a-input v-model:value="dictFormModel.dictKey" placeholder="输入数据字典标识" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <!-- <a-form-item label="多选" name="multipleChoice">
              <a-radio-group v-model:value="dictFormModel.multipleChoice">
                <a-radio-button value="y">是</a-radio-button>
                <a-radio-button value="n">否</a-radio-button>
              </a-radio-group>
            </a-form-item> -->
            <Dict v-model="dictFormModel.multipleChoice" dictKey="dictMultipleChoice" formItemName="multipleChoice" />
          </a-col>
          <a-col :span="12">
            <!-- <a-form-item label="值类型" name="valueType">
              <a-radio-group v-model:value="dictFormModel.valueType">
                <a-radio-button value="list">列表</a-radio-button>
                <a-radio-button value="tree">树</a-radio-button>
              </a-radio-group>
            </a-form-item> -->
            <Dict v-model="dictFormModel.valueType" dictKey="dictValueType" formItemName="valueType" />
          </a-col>
          <a-col :span="24">
            <!-- <a-form-item label="组件类型" name="assemblyType">
              <a-radio-group v-model:value="dictFormModel.assemblyType">
                <a-radio-button value="Select">选择器</a-radio-button>
                <a-radio-button value="Radio">单选框</a-radio-button>
                <a-radio-button value="Checkbox">多选框</a-radio-button>
                <a-radio-button value="TreeSelect">树选择器</a-radio-button>
              </a-radio-group>
            </a-form-item> -->
            <Dict v-model="dictFormModel.assemblyType" dictKey="dictAssemblyType" formItemName="assemblyType" />
          </a-col>
          <a-col :span="24">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="dictFormModel.weightOrder" :style="{ ...coreStyle.width(200) }" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="描述" name="description">
              <a-textarea v-model:value="dictFormModel.description" :rows="3" placeholder="输入描述" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button @click="dictFormReset">
            <CoreIcon icon="ant-design:clear-outlined" :size="14" />
            重置
          </a-button>
          <a-button type="primary" ghost :loading="dictFormSaveSubmitting" @click="dictFormNextAdd">
            <CoreIcon icon="tabler:player-track-next" :size="14" />
            继续录入
          </a-button>
          <a-button type="primary" ghost :loading="dictFormSaveSubmitting" @click="dictFormSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="dictDetailDrawerVisible"
      title="数据字典详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <a-descriptions-item label="数据字典标识" :span="1">{{ dictDetailModel.dictKey }}</a-descriptions-item>
          <a-descriptions-item label="数据字典名称" :span="1">{{ dictDetailModel.dictName }}</a-descriptions-item>
          <a-descriptions-item label="多选" :span="1">{{ coreToolStore.getDictDisplayName('dictMultipleChoice', dictDetailModel.multipleChoice) }}</a-descriptions-item>
          <a-descriptions-item label="值类型" :span="1">{{ coreToolStore.getDictDisplayName('dictValueType', dictDetailModel.valueType) }}</a-descriptions-item>
          <a-descriptions-item label="组件类型" :span="1">{{ coreToolStore.getDictDisplayName('dictAssemblyType', dictDetailModel.assemblyType) }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="1">{{ dictDetailModel.weightOrder }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="2">{{ dictDetailModel.description }}</a-descriptions-item>
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" @click="dictDetailUpdate">
            <CoreIcon icon="ri:brush-line" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="dictValueEditDrawerVisible"
      title="编辑数据字典值信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="dictValueEditDrawerOnClose"
    >
      <a-form ref="dictValueFormRef" :model="dictValueFormModel" :rules="dictValueFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="所属数据字典" name="fkid">
              <!-- <a-input v-model:value="dictValueFormModel.fkid" placeholder="输入外键Id" /> -->
              <a-select v-model:value="dictValueFormModel.fkid" optionFilterProp="title" show-search allowClear disabled placeholder="请选择" :style="{ ...coreStyle.width('100%') }">
                <a-select-option v-for="item in dictValueFkidSelectData" :value="item.id" :key="item.id" :title="item.dictName + ' - ' + item.dictKey">
                  {{ item.dictName + ' - ' + item.dictKey }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="dictValueFormModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="'tree' === dictListSelectCurrentRowData.valueType">
            <a-form-item label="父级数据字典值" name="pid">
              <!-- <a-input v-model:value="dictValueFormModel.pid" placeholder="输入父级Id" /> -->
              <a-select v-model:value="dictValueFormModel.pid" optionFilterProp="title" show-search allowClear placeholder="请选择" :style="{ ...coreStyle.width('100%') }">
                <a-select-option v-for="item in dictValuePidSelectData" :value="item.id" :key="item.id" :title="item.displayName + ' - ' + item.codeName">
                  {{ item.displayName + ' - ' + item.codeName }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="'tree' === dictListSelectCurrentRowData.valueType">
            <a-form-item label="值类型" name="valueType">
              <a-input v-model:value="dictValueFormModel.valueType" placeholder="输入值类型" />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="'tree' === dictListSelectCurrentRowData.valueType">
            <a-form-item label="值类型名称" name="valueTypeName">
              <a-input v-model:value="dictValueFormModel.valueTypeName" placeholder="输入值类型名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="显示名称" name="displayName">
              <a-input v-model:value="dictValueFormModel.displayName" placeholder="输入显示名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="代码 (不能重复)" name="codeName">
              <a-input v-model:value="dictValueFormModel.codeName" placeholder="输入代码" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="描述" name="description">
              <a-textarea v-model:value="dictValueFormModel.description" :rows="3" placeholder="输入描述" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button @click="dictValueFormReset">
            <CoreIcon icon="ant-design:clear-outlined" :size="14" />
            重置
          </a-button>
          <a-button type="primary" ghost :loading="dictValueFormSaveSubmitting" @click="dictValueFormNextAdd">
            <CoreIcon icon="tabler:player-track-next" :size="14" />
            继续录入
          </a-button>
          <a-button type="primary" ghost :loading="dictValueFormSaveSubmitting" @click="dictValueFormSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="dictValueDetailDrawerVisible"
      title="数据字典值详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <!-- <a-descriptions-item label="外键Id" :span="1">{{ dictValueDetailModel.fkid }}</a-descriptions-item> -->
          <!-- <a-descriptions-item label="父级Id" :span="1">{{ dictValueDetailModel.pid }}</a-descriptions-item> -->
          <a-descriptions-item label="代码" :span="1">{{ dictValueDetailModel.codeName }}</a-descriptions-item>
          <a-descriptions-item label="显示名称" :span="1">{{ dictValueDetailModel.displayName }}</a-descriptions-item>
          <a-descriptions-item label="值类型" :span="1" v-if="'tree' === dictListSelectCurrentRowData.valueType">{{ dictValueDetailModel.valueType }}</a-descriptions-item>
          <a-descriptions-item label="值类型名称" :span="1" v-if="'tree' === dictListSelectCurrentRowData.valueType">{{ dictValueDetailModel.valueTypeName }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="2">{{ dictValueDetailModel.weightOrder }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="2">{{ dictValueDetailModel.description }}</a-descriptions-item>
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" @click="dictValueDetailUpdate">
            <CoreIcon icon="ri:brush-line" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapStores } from 'pinia'
import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'

import { useDictStore } from '/@/store/modules/cn/core/dict/dictStore'

import type { Dict } from '/@/model/cn/core/dict/eo/Dict'
import { toDict } from '/@/model/cn/core/dict/eo/Dict'

import type { DictQO } from '/@/model/cn/core/dict/qo/DictQO'
import { toDictQO } from '/@/model/cn/core/dict/qo/DictQO'

import type { DictVO } from '/@/model/cn/core/dict/vo/DictVO'
import { toDictVO } from '/@/model/cn/core/dict/vo/DictVO'

import { useDictValueStore } from '/@/store/modules/cn/core/dict/dictValueStore'

import type { DictValue } from '/@/model/cn/core/dict/eo/DictValue'
import { toDictValue } from '/@/model/cn/core/dict/eo/DictValue'

import type { DictValueQO } from '/@/model/cn/core/dict/qo/DictValueQO'
import { toDictValueQO } from '/@/model/cn/core/dict/qo/DictValueQO'

import type { DictValueVO } from '/@/model/cn/core/dict/vo/DictValueVO'
import { toDictValueVO } from '/@/model/cn/core/dict/vo/DictValueVO'

@Options({
  name: 'CoreDict',
  components: {
  },
  computed: {
    ...mapStores(useCoreToolStore, useDictStore, useDictValueStore),
  },
})
export default class CoreDict extends Vue {

  dictListDataSet: Array<DictVO> = []

  dictListColumns: Array<object> = [
    {
      title: '数据字典名称',
      dataIndex: 'dictName',
      align: 'center',
      ellipsis: true,
      // width: 200,
    },
    {
      title: '数据字典标识',
      dataIndex: 'dictKey',
      align: 'center',
      ellipsis: true,
      // width: 200,
    },
    {
      title: '多选',
      dataIndex: 'multipleChoice',
      align: 'center',
      slots: { customRender: 'multipleChoiceData' },
      // ellipsis: true,
      width: 100,
    },
    {
      title: '值类型',
      dataIndex: 'valueType',
      align: 'center',
      slots: { customRender: 'valueTypeData' },
      // ellipsis: true,
      width: 100,
    },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      // ellipsis: true,
      width: 100,
    },
  ]

  dictListLoading: boolean = false

  dictListSelectedRowKeys: string[] = []

  dictListSelectData: Array<DictVO> = []

  dictListSelectLength: number = 0

  dictListSelectCurrentRowIndex: number = -1

  dictListSelectCurrentRowData: DictVO = toDictVO()

  dictListPaginationVisible: boolean = true

  dictListDataPage: number = 1

  dictListDataRows: number = 10

  dictListDataCount: number = 0

  dictListRowOnChange(selectedRowKeys, selectedRows) {
    this.dictListSelectedRowKeys = [ ...selectedRowKeys ]
    this.dictListSelectData = [ ...selectedRows ]
    this.dictListSelectLength = selectedRows.length
  }

  dictListCustomRow(record, index) {
    return {
      'class': index === this.dictListSelectCurrentRowIndex ? 'ant-table-row-selected' : '',
      onClick: (event) => {
        this.dictListSelectCurrentRowIndex = index
        this.dictListSelectCurrentRowData = { ...record }
        this.dictValueFormQueryModel.fkid = this.dictListSelectCurrentRowData.id
        this.dictValueFormQuerySearch()
        // let selectedRowKeys = [ ...this.dictListSelectedRowKeys ]
        // let selectedRows = [ ...this.dictListSelectData ]
        // if (selectedRowKeys.includes(record.id)) {
        //   this.dictListRowOnChange([ ...selectedRowKeys.filter(eo => { return eo !== record.id }) ], [ ...selectedRows.filter(eo => { return eo.id !== record.id }) ])
        // } else {
        //   selectedRowKeys.push(record.id)
        //   selectedRows.push(record)
        //   this.dictListRowOnChange([ ...new Set(selectedRowKeys) ], [ ...new Set(selectedRows) ])
        // }
      },
      onDblclick: (event) => {
        this.dictStore.pkVo(this.$toParam<DictQO>({ id: this.dictListSelectCurrentRowData.id } as DictQO)).then((res: Res<DictVO>) => {
          if (res.succ) {
            this.dictDetailModel = { ...res.data[0] }
            this.dictDetailDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      },
    }
  }

  dictListGetDataSet() {
    this.dictListLoading = true
    this.dictListRowOnChange([], [])
    this.dictListSelectCurrentRowIndex = -1
    this.dictListSelectCurrentRowData = toDictVO()
    this.dictStore.pagingVo(this.$toParam<DictQO>({ weightOrderAsc: 'y' } as DictQO, this.dictListDataPage, this.dictListDataRows)).then((res: Res<DictVO>) => {
      if (res.succ) {
        this.dictListPaginationVisible = true
        this.dictListDataSet = [ ...res.data ]
        this.dictListDataCount = res.count
        setTimeout(() => { this.dictListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.dictListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.dictListLoading = false }, 500)
    })
    this.dictValueFormQuerySearch()
  }

  dictListPaginationChange() {
    this.dictListGetDataSet()
  }

  dictListPaginationShowSizeChange() {
    this.dictListGetDataSet()
  }

  dictFormQueryVisible: boolean = false

  dictFormQuerySubmitting: boolean = false

  dictFormQueryModel: DictQO = toDictQO()

  dictFormQueryUpDown() {
    this.dictFormQueryVisible = !this.dictFormQueryVisible
  }

  dictFormQueryReset() {
    if (this.$refs.dictFormQueryRef) {
      this.$refs.dictFormQueryRef.resetFields()
      this.dictFormQueryModel = toDictQO()
    }
  }

  dictFormQuerySearch() {
    this.dictFormQuerySubmitting = true
    this.dictListLoading = true
    this.dictListRowOnChange([], [])
    this.dictListSelectCurrentRowIndex = -1
    this.dictListSelectCurrentRowData = toDictVO()
    this.dictStore.queryVo(this.$toParam<DictQO>(this.$toSearch({ ...this.dictFormQueryModel }))).then((res: Res<DictVO>) => {
      if (res.succ) {
        this.dictListPaginationVisible = false
        this.dictListDataSet = [ ...res.data ]
        this.dictListDataCount = res.count
        setTimeout(() => { this.dictFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.dictListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.dictFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.dictListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.dictFormQuerySubmitting = false }, 5000)
      setTimeout(() => { this.dictListLoading = false }, 500)
    })
    this.dictValueFormQuerySearch()
  }

  dictEditDrawerVisible: boolean = false

  dictFormSaveSubmitting: boolean = false

  dictFormSaveContinue: boolean = false

  dictFormModel: Dict = toDict()

  dictFormRules: object = {
    dictKey: [
      { type: 'string', required: true, message: '数据字典标识不能为空！', trigger: ['change', 'blur'] },
    ],
    dictName: [
      { type: 'string', required: true, message: '数据字典名称不能为空！', trigger: ['change', 'blur'] },
    ],
    multipleChoice: [
      { type: 'string', required: true, message: '多选不能为空！', trigger: ['change', 'blur'] },
    ],
    valueType: [
      { type: 'string', required: true, message: '值类型不能为空！', trigger: ['change', 'blur'] },
    ],
    assemblyType: [
      { type: 'string', required: true, message: '组件类型不能为空！', trigger: ['change', 'blur'] },
    ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
  }

  dictFormReset() {
    if (this.$refs.dictFormRef) {
      this.$refs.dictFormRef.resetFields()
      this.dictFormModel = toDict()
    }
  }

  dictFormSubmit() {
    this.dictFormSaveSubmitting = true
    this.$refs.dictFormRef.validate().then(() => {
      if (this.dictFormModel.id) {
        this.dictStore.update(this.$toParam<Dict>(this.dictFormModel)).then((res: Res<Dict>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.dictFormReset()
            this.dictFormModel.multipleChoice = 'n'
            this.dictFormModel.valueType = 'list'
            this.dictFormModel.assemblyType = 'Select'
            this.dictFormSaveSubmitting = false
            if (!this.dictFormSaveContinue) {
              this.dictEditDrawerVisible = false
              this.dictListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.dictFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.dictFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.dictStore.save(this.$toParam<Dict>(this.dictFormModel)).then((res: Res<Dict>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.dictFormReset()
            this.dictFormModel.multipleChoice = 'n'
            this.dictFormModel.valueType = 'list'
            this.dictFormModel.assemblyType = 'Select'
            this.dictFormSaveSubmitting = false
            if (!this.dictFormSaveContinue) {
              this.dictEditDrawerVisible = false
              this.dictListGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.dictFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.dictFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.dictFormSaveSubmitting = false }, 3000)
    })
  }

  dictFormSave() {
    this.dictFormSaveContinue = false
    this.dictFormSubmit()
  }

  dictFormNextAdd() {
    this.dictFormSaveContinue = true
    this.dictFormSubmit()
  }

  dictAdd() {
    if (this.dictFormModel.id) {
      this.dictFormReset()
    }
    this.dictFormModel.multipleChoice = 'n'
    this.dictFormModel.valueType = 'list'
    this.dictFormModel.assemblyType = 'Select'
    this.dictEditDrawerVisible = true
  }

  dictUpdate() {
    this.dictStore.pk(this.$toParam<DictQO>({ id: this.dictListSelectData[0].id } as DictQO)).then((res: Res<Dict>) => {
      if (res.succ) {
        this.dictFormReset()
        this.dictFormModel = { ...res.data[0] }
        this.dictEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dictEditDrawerOnClose() {
    if (this.dictFormSaveContinue) {
      this.dictFormSaveContinue = false
      this.dictListGetDataSet()
    }
  }

  dictDelete() {
    let param: Array<Dict> = []
    this.dictListSelectData.forEach((eo: DictVO) => {
      param.push({ id: eo.id } as Dict)
    })
    this.dictStore.removeBatch(this.$toParam<Dict>(param)).then((res: Res<Dict>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.dictListGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dictDetailDrawerVisible: boolean = false

  dictDetailModel: DictVO = toDictVO()

  dictDetailInfo() {
    this.dictStore.pkVo(this.$toParam<DictQO>({ id: this.dictListSelectData[0].id } as DictQO)).then((res: Res<DictVO>) => {
      if (res.succ) {
        this.dictDetailModel = { ...res.data[0] }
        this.dictDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dictDetailUpdate() {
    this.dictStore.pk(this.$toParam<DictQO>({ id: this.dictDetailModel.id } as DictQO)).then((res: Res<Dict>) => {
      if (res.succ) {
        this.dictFormReset()
        this.dictFormModel = { ...res.data[0] }
        this.dictDetailDrawerVisible = false
        this.dictEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  getMultipleChoiceDisplayName(value) {
    if ('y' === value) {
      return '是'
    } else if ('n' === value) {
      return '否'
    } else {
      return value
    }
  }

  getValueTypeDisplayName(value) {
    if ('list' === value) {
      return '列表'
    } else if ('tree' === value) {
      return '树'
    } else {
      return value
    }
  }

  getAssemblyTypeDisplayName(value) {
    if ('Select' === value) {
      return '选择器'
    } else if ('Radio' === value) {
      return '单选框'
    } else if ('Checkbox' === value) {
      return '多选框'
    } else if ('TreeSelect' === value) {
      return '树选择器'
    } else {
      return value
    }
  }

  dictValueListDataSet: Array<DictValueVO> = []

  dictValueListColumns: Array<object> = [
    {
      title: '显示名称',
      dataIndex: 'displayName',
      align: 'center',
      ellipsis: true,
      // width: 200,
    },
    {
      title: '代码',
      dataIndex: 'codeName',
      align: 'center',
      ellipsis: true,
      // width: 200,
    },
    {
      title: '值类型',
      dataIndex: 'valueTypeName',
      align: 'center',
      ellipsis: true,
      width: 100,
    },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      // ellipsis: true,
      width: 100,
    },
  ]

  dictValueListLoading: boolean = false

  dictValueListSelectedRowKeys: string[] = []

  dictValueListSelectData: Array<DictValueVO> = []

  dictValueListSelectLength: number = 0

  dictValueListSelectCurrentRowIndex: number = -1

  dictValueListSelectCurrentRowData: DictValueVO = toDictValueVO()

  dictValueListPaginationVisible: boolean = true

  dictValueListDataPage: number = 1

  dictValueListDataRows: number = 10

  dictValueListDataCount: number = 0

  dictValueListRowOnChange(selectedRowKeys, selectedRows) {
    this.dictValueListSelectedRowKeys = [ ...selectedRowKeys ]
    this.dictValueListSelectData = [ ...selectedRows ]
    this.dictValueListSelectLength = selectedRows.length
  }

  dictValueListCustomRow(record, index) {
    return {
      'class': index === this.dictValueListSelectCurrentRowIndex ? 'ant-table-row-selected' : '',
      onClick: (event) => {
        this.dictValueListSelectCurrentRowIndex = index
        this.dictValueListSelectCurrentRowData = { ...record }
        let selectedRowKeys = [ ...this.dictValueListSelectedRowKeys ]
        let selectedRows = [ ...this.dictValueListSelectData ]
        if (selectedRowKeys.includes(record.id)) {
          this.dictValueListRowOnChange([ ...selectedRowKeys.filter(eo => { return eo !== record.id }) ], [ ...selectedRows.filter(eo => { return eo.id !== record.id }) ])
        } else {
          selectedRowKeys.push(record.id)
          selectedRows.push(record)
          this.dictValueListRowOnChange([ ...new Set(selectedRowKeys) ], [ ...new Set(selectedRows) ])
        }
      },
      onDblclick: (event) => {
        this.dictValueStore.pkVo(this.$toParam<DictValueQO>({ id: this.dictValueListSelectCurrentRowData.id } as DictValueQO)).then((res: Res<DictValueVO>) => {
          if (res.succ) {
            this.dictValueDetailModel = { ...res.data[0] }
            this.dictValueDetailDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      },
    }
  }

  dictValueListGetDataSet() {
    this.dictValueListLoading = true
    this.dictValueListRowOnChange([], [])
    this.dictValueListSelectCurrentRowIndex = -1
    this.dictValueListSelectCurrentRowData = toDictValueVO()
    this.dictValueStore.pagingVo(this.$toParam<DictValueQO>({ weightOrderAsc: 'y' } as DictValueQO, this.dictValueListDataPage, this.dictValueListDataRows)).then((res: Res<DictValueVO>) => {
      if (res.succ) {
        this.dictValueListPaginationVisible = true
        this.dictValueListDataSet = [ ...res.data ]
        this.dictValueListDataCount = res.count
        setTimeout(() => { this.dictValueListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.dictValueListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.dictValueListLoading = false }, 500)
    })
  }

  dictValueListPaginationChange() {
    this.dictValueListGetDataSet()
  }

  dictValueListPaginationShowSizeChange() {
    this.dictValueListGetDataSet()
  }

  dictValueFormQueryVisible: boolean = false

  dictValueFormQuerySubmitting: boolean = false

  dictValueFormQueryModel: DictValueQO = toDictValueQO()

  dictValueFormQueryUpDown() {
    this.dictValueFormQueryVisible = !this.dictValueFormQueryVisible
  }

  dictValueFormQueryReset() {
    if (this.$refs.dictValueFormQueryRef) {
      this.$refs.dictValueFormQueryRef.resetFields()
      this.dictValueFormQueryModel = toDictValueQO()
      this.dictValueFormQueryModel.weightOrderAsc = 'y'
    }
  }

  dictValueFormQuerySearch() {
    this.dictValueFormQuerySubmitting = true
    this.dictValueListLoading = true
    this.dictValueListRowOnChange([], [])
    this.dictValueListSelectCurrentRowIndex = -1
    this.dictValueListSelectCurrentRowData = toDictValueVO()
    if (this.dictListSelectCurrentRowData.id) {
      this.dictValueStore.queryVo(this.$toParam<DictValueQO>(this.$toSearch({ ...this.dictValueFormQueryModel }))).then((res: Res<DictValueVO>) => {
        if (res.succ) {
          this.dictValueListPaginationVisible = false
          this.dictValueListDataSet = [ ...res.data ]
          this.dictValueListDataCount = res.count
          setTimeout(() => { this.dictValueFormQuerySubmitting = false }, 5000)
          setTimeout(() => { this.dictValueListLoading = false }, 500)
        } else {
          this.$message.error(res.msg, 6)
          setTimeout(() => { this.dictValueFormQuerySubmitting = false }, 5000)
          setTimeout(() => { this.dictValueListLoading = false }, 500)
        }
      }).catch(err => {
        setTimeout(() => { this.dictValueFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.dictValueListLoading = false }, 500)
      })
    } else {
      this.dictValueListPaginationVisible = false
      this.dictValueListDataSet = []
      this.dictValueListDataCount = 0
      setTimeout(() => { this.dictValueFormQuerySubmitting = false }, 5000)
      setTimeout(() => { this.dictValueListLoading = false }, 500)
    }
  }

  dictValueEditDrawerVisible: boolean = false

  dictValueFormSaveSubmitting: boolean = false

  dictValueFormSaveContinue: boolean = false

  dictValueFormModel: DictValue = toDictValue()

  dictValueFormRules: object = {
    codeName: [
      { type: 'string', required: true, message: '代码不能为空！', trigger: ['change', 'blur'] },
    ],
    displayName: [
      { type: 'string', required: true, message: '显示名称不能为空！', trigger: ['change', 'blur'] },
    ],
    // valueType: [
    //   { type: 'string', required: true, message: '值类型不能为空！', trigger: ['change', 'blur'] },
    // ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
    // pid: [
    //   { type: 'string', required: true, message: '父级Id不能为空！', trigger: ['change', 'blur'] },
    // ],
    fkid: [
      { type: 'string', required: true, message: '外键Id不能为空！', trigger: ['change', 'blur'] },
    ],
  }

  dictValueFkidSelectData: Array<Dict> = []

  dictValuePidSelectData: Array<DictValue> = []

  dictValueFormReset() {
    if (this.$refs.dictValueFormRef) {
      this.$refs.dictValueFormRef.resetFields()
      this.dictValueFormModel = toDictValue()
      this.dictValueFormModel.fkid = this.dictListSelectCurrentRowData.id
    }
  }

  dictValueFormSubmit() {
    this.dictValueFormSaveSubmitting = true
    this.$refs.dictValueFormRef.validate().then(() => {
      if (this.dictValueFormModel.id) {
        this.dictValueStore.update(this.$toParam<DictValue>(this.dictValueFormModel)).then((res: Res<DictValue>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.dictValueFormReset()
            this.dictValueFormSaveSubmitting = false
            // this.dictStore.query(this.$toParam<DictQO>({})).then((res: Res<Dict>) => {
            //   if (res.succ) {
            //     this.dictValueFkidSelectData = [ ...res.data ]
            //   } else {
            //     this.$message.error(res.msg, 6)
            //   }
            // })
            this.dictValueStore.query(this.$toParam<DictValueQO>({ fkid: this.dictListSelectCurrentRowData.id, weightOrderAsc: 'y' } as DictValueQO)).then((res: Res<DictValue>) => {
              if (res.succ) {
                this.dictValuePidSelectData = [ ...res.data ]
              } else {
                this.$message.error(res.msg, 6)
              }
            })
            this.dictValueFormModel.fkid = this.dictListSelectCurrentRowData.id
            if (!this.dictValueFormSaveContinue) {
              this.dictValueEditDrawerVisible = false
              this.dictValueFormQuerySearch()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.dictValueFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.dictValueFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.dictValueStore.save(this.$toParam<DictValue>(this.dictValueFormModel)).then((res: Res<DictValue>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.dictValueFormReset()
            this.dictValueFormSaveSubmitting = false
            // this.dictStore.query(this.$toParam<DictQO>({})).then((res: Res<Dict>) => {
            //   if (res.succ) {
            //     this.dictValueFkidSelectData = [ ...res.data ]
            //   } else {
            //     this.$message.error(res.msg, 6)
            //   }
            // })
            this.dictValueStore.query(this.$toParam<DictValueQO>({ fkid: this.dictListSelectCurrentRowData.id, weightOrderAsc: 'y' } as DictValueQO)).then((res: Res<DictValue>) => {
              if (res.succ) {
                this.dictValuePidSelectData = [ ...res.data ]
              } else {
                this.$message.error(res.msg, 6)
              }
            })
            this.dictValueFormModel.fkid = this.dictListSelectCurrentRowData.id
            if (!this.dictValueFormSaveContinue) {
              this.dictValueEditDrawerVisible = false
              this.dictValueFormQuerySearch()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.dictValueFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.dictValueFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.dictValueFormSaveSubmitting = false }, 3000)
    })
  }

  dictValueFormSave() {
    this.dictValueFormSaveContinue = false
    this.dictValueFormSubmit()
  }

  dictValueFormNextAdd() {
    this.dictValueFormSaveContinue = true
    this.dictValueFormSubmit()
  }

  dictValueAdd() {
    if (this.dictValueFormModel.id) {
      this.dictValueFormReset()
    }
    this.dictStore.query(this.$toParam<DictQO>({})).then((res: Res<Dict>) => {
      if (res.succ) {
        this.dictValueFkidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.dictValueStore.query(this.$toParam<DictValueQO>({ fkid: this.dictListSelectCurrentRowData.id, weightOrderAsc: 'y' } as DictValueQO)).then((res: Res<DictValue>) => {
      if (res.succ) {
        this.dictValuePidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.dictValueFormModel.fkid = this.dictListSelectCurrentRowData.id
    this.dictValueEditDrawerVisible = true
  }

  dictValueUpdate() {
    this.dictStore.query(this.$toParam<DictQO>({})).then((res: Res<Dict>) => {
      if (res.succ) {
        this.dictValueFkidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.dictValueStore.query(this.$toParam<DictValueQO>({ fkid: this.dictListSelectCurrentRowData.id, weightOrderAsc: 'y' } as DictValueQO)).then((res: Res<DictValue>) => {
      if (res.succ) {
        this.dictValuePidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.dictValueStore.pk(this.$toParam<DictValueQO>({ id: this.dictValueListSelectData[0].id } as DictValueQO)).then((res: Res<DictValue>) => {
      if (res.succ) {
        this.dictValueFormReset()
        this.dictValueFormModel = { ...res.data[0] }
        this.dictValueEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dictValueEditDrawerOnClose() {
    if (this.dictValueFormSaveContinue) {
      this.dictValueFormSaveContinue = false
      this.dictValueFormQuerySearch()
    }
  }

  dictValueDelete() {
    let param: Array<DictValue> = []
    this.dictValueListSelectData.forEach((eo: DictValueVO) => {
      param.push({ id: eo.id } as DictValue)
    })
    this.dictValueStore.removeBatch(this.$toParam<DictValue>(param)).then((res: Res<DictValue>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.dictValueFormQuerySearch()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dictValueDetailDrawerVisible: boolean = false

  dictValueDetailModel: DictValueVO = toDictValueVO()

  dictValueDetailInfo() {
    this.dictValueStore.pkVo(this.$toParam<DictValueQO>({ id: this.dictValueListSelectData[0].id } as DictValueQO)).then((res: Res<DictValueVO>) => {
      if (res.succ) {
        this.dictValueDetailModel = { ...res.data[0] }
        this.dictValueDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  dictValueDetailUpdate() {
    this.dictStore.query(this.$toParam<DictQO>({})).then((res: Res<Dict>) => {
      if (res.succ) {
        this.dictValueFkidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.dictValueStore.query(this.$toParam<DictValueQO>({ fkid: this.dictListSelectCurrentRowData.id, weightOrderAsc: 'y' } as DictValueQO)).then((res: Res<DictValue>) => {
      if (res.succ) {
        this.dictValuePidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.dictValueStore.pk(this.$toParam<DictValueQO>({ id: this.dictValueDetailModel.id } as DictValueQO)).then((res: Res<DictValue>) => {
      if (res.succ) {
        this.dictValueFormReset()
        this.dictValueFormModel = { ...res.data[0] }
        this.dictValueDetailDrawerVisible = false
        this.dictValueEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  mounted() {
    this.dictValueFormQueryModel.weightOrderAsc = 'y'
    this.dictListGetDataSet()
  }

}
</script>

<style scoped lang="less">

</style>
